package com.controllers
{
	import com.data.CSVParser;
	import com.data.FilterData;
	import com.data.IdFilterData;
	import com.data.Score;
	import com.data.ScoreFilterData;
	
	import flash.filesystem.File;
	
	import mx.collections.ArrayCollection;

	[Bindable]
	public class IndManager
	{
		private var csvParser:CSVParser;
		private var _inds:Array;
		
		public var filtersData:ArrayCollection = new ArrayCollection();
		
		public var scoreNames:Array;
		private var scoreTypes:Array;
		public var scores:ArrayCollection = new ArrayCollection();
		public var scoreLabels:Array;
		
		private var paretoRank:ParetoRanking;
		
		[Embed(source="assets/pareto.csv", mimeType="application/octet-stream")]
		private var FileClass : Class;
		private var obj:Object = new FileClass();
		
		public function IndManager(paretoFile:String)
		{
			this.csvParser = new CSVParser();
			
			//this.csvParser = new CSVParser(File.documentsDirectory.resolvePath(paretoFile));
			//_inds = this.csvParser.parseFile(new File(paretoFile));
			
			_inds = this.csvParser.parseString(obj.toString());
			
			scoreTypes = [Score.MIN, Score.MAX, Score.MIN];
			scoreNames = ["cost", "daylight", "energy"];	
			scoreLabels = ["Cost", "Day Light", "Energy"];
			setFilterData();
			
			for (var i:int = 0; i < scoreNames.length; i++)
				scores.addItem(new Score(scoreNames[i], scoreTypes[i], scoreLabels[i]));
			paretoRank = new ParetoRanking(scores);
			updateFilterMinMax();
		}

		private function updateFilterMinMax():void
		{
			if (_inds.length == 0) return;
			for (var i:int = 0; i < filtersData.length; i++) 
			{
				var filterData:FilterData = filtersData[i] as FilterData;
				var fname:String = filterData.fieldName; // field name
				var min:Number = _inds[0][fname];
				var max:Number = min;
				
				for (var j:int = 1; j < _inds.length; j++) 
				{
					if (_inds[j][fname] < min)
						min = _inds[j][fname]
					if (_inds[j][fname] > max)
						max = _inds[j][fname];
				} // end for
				filterData.maxVal = max;
				filterData.minVal = min;
			} // end for
		}
		
		private function setFilterData():void
		{					
			filtersData.addItem(new IdFilterData("id", "birth_id", "death_id"));
			for (var i:int = 0; i < this.scoreNames.length; i++)
				filtersData.addItem(new ScoreFilterData(scoreNames[i], scoreLabels[i]));
		}
		public function get indsCopy():Array
		{
			var res:Array = new Array(_inds.length);
			for (var i:Number = 0; i < _inds.length; i++)
				res[i] = _inds[i];
			return res;
		}
		
		public function get inds():ArrayCollection
		{
			return _inds as ArrayCollection;
		}
		
		public function filteredInds():ArrayCollection
		{
			var res:ArrayCollection = new ArrayCollection();
			for each(var ind:Object in _inds) {
				var isOk:Boolean = true;
				for each(var filterData:FilterData in filtersData)
				{
					if (filterData.isOk(ind) == false)
					{
						isOk = false;
						break;
					}
				}
				if (isOk)
					res.addItem(ind);
			}
			return res;
		}
		
		public function paretoFrontInds(paretoData:ArrayCollection):ArrayCollection
		{
			var frontData:ArrayCollection = new ArrayCollection();
			var rankedData:Array = paretoRank.findFront(paretoData);
			
			for (var i:int = paretoData.length-1; i >= 0; i--)
				if (rankedData[i] == ParetoRanking.FRONT) {
					frontData.addItem(paretoData[i]);
					paretoData.removeItemAt(i);
				}			
		 	return frontData;
		}
	}
}